package logic

import (
	"context"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"month/rpc/internal/svc"
	"month/rpc/month"

	"github.com/zeromicro/go-zero/core/logx"
)

type FileUpLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFileUpLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileUpLogic {
	return &FileUpLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 文件上传
func (l *FileUpLogic) FileUp(in *month.FileUpRequest) (*month.EsResponse, error) {
	// todo: add your logic here and delete this line

	db, err := gorm.Open(mysql.Open("root:q025l6263@tcp(43.143.123.142:3306)/2107a"), &gorm.Config{})
	if err != nil {
		return &month.EsResponse{
			Code:    400,
			Message: "数据库连接失败",
		}, nil
	}

	err = db.Exec("update `fgit` set `vl` = ? where `name`= ?", in.Vl, in.Name).Error
	if err != nil {
		return &month.EsResponse{
			Code:    400,
			Message: "数据库修改失败",
		}, nil
	}

	var gid int64
	db.Raw("selsct `id` from `fgit` where `name` = ?", in.Name).Scan(&gid)
	err = db.Exec("insert into `fgit_his` (`vl`,`url`,`gid`) values (?,?,?)", in.Vl, in.File, gid).Error
	if err != nil {
		return &month.EsResponse{
			Code:    400,
			Message: "数据库添加失败",
		}, nil
	}

	return &month.EsResponse{
		Code:    200,
		Message: "成功",
		Data:    in.File,
	}, nil
}
